package com.sinosoft.perfect.system._display.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

import com.sinosoft.perfect.common.system.BaseAction;
import com.sinosoft.perfect.system._.po.TDisplay;
import com.sinosoft.perfect.system._.po.TUser;
import com.sinosoft.perfect.system._display.dataFormat.IDisplayGridDataFormat;
import com.sinosoft.perfect.system._display.service.IDisplayService;
import com.sinosoft.perfect.system._user.service.IUserService;

@SuppressWarnings("serial")
public class DisplayAction extends BaseAction {

	private IDisplayService displayService;

	private IUserService userService;

	private IDisplayGridDataFormat displayGridDataFormat;

	private TDisplay tdisplay;

	private String userId;


	public IDisplayService getDisplayService() {
		return displayService;
	}

	public void setDisplayService(IDisplayService displayService) {
		this.displayService = displayService;
	}

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public IDisplayGridDataFormat getDisplayGridDataFormat() {
		return displayGridDataFormat;
	}

	public void setDisplayGridDataFormat(
			IDisplayGridDataFormat displayGridDataFormat) {
		this.displayGridDataFormat = displayGridDataFormat;
	}

	public TDisplay getTdisplay() {
		return tdisplay;
	}

	public void setTdisplay(TDisplay tdisplay) {
		this.tdisplay = tdisplay;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String displayGrid() throws IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		String resultXml = null;
		try {
			List<TDisplay> displays = displayService.selectByTDisplay(tdisplay);
			resultXml = displayGridDataFormat.dataFormatDisplayGrid(displays, getSelectDisplayId());
		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		PrintWriter out = response.getWriter();
		out.print(resultXml);
		out.flush();
		out.close();
		return null;
	}

	private String getSelectDisplayId() throws SQLException{
		String selectDisplayId = null;
		if(userId != null){
			TUser user = new TUser();
			user.setUserMainId(userId);
			List<TUser> users = userService.selectTUser(user);
			if (users != null && users.size() != 0) {
				selectDisplayId = users.get(0).getFrameModelId();
			}
		}
		return selectDisplayId;
	}
}
